package com.luoha.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.luoha.excel.ExcelHandler;
import com.luoha.lottery.Employee;
import com.luoha.lottery.LotteryActive;

import net.sf.json.JSONObject;

@WebServlet("/LotteryServlet")
public class LotteryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static String Excel_PATH = null;
	
	private final int countNum = 201;
	
	private List<Employee> employeeList = new ArrayList<Employee>();
	
	private List<Integer> lotteryList = new ArrayList<Integer>();
	
//	private List<Employee> extractList 
//				= Collections.synchronizedList(new ArrayList<Employee>());
	
	private List<Employee> extractList = new ArrayList<Employee>(); 
    
    @Override
    public void init() throws ServletException {
    	initData();
    }  
    
    public List<Employee> getExtractList(){
		return extractList;
	}
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("This is doGet function");
		String action = request.getParameter("action");
		if(action.equals("lottery")){
			Map<String,Object> resMap = new HashMap<String,Object>();
			boolean bOper = true;
			String message = null;
			String empId = request.getParameter("empId").trim();
			String empName = request.getParameter("empName").trim();
			Employee employee = null;
			try {
				employee = LotteryActive.extractLottery(empId,empName,employeeList,lotteryList, extractList);
			} catch (Exception e) {
				e.printStackTrace();
				bOper = false;
				message = e.getMessage();
			}
			if(bOper){
				resMap.put("result", "success");
				resMap.put("employee", employee);
			}else{
				resMap.put("result", "fail");
				resMap.put("message", message);
			}
			JSONObject json = new JSONObject();
			json.put("data", resMap);
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.write(json.toString());;
			out.close();
		}else if(action.equals("query")){
			List<Employee> list = getExtractList();
			JSONObject json = new JSONObject();
			json.put("employeeList", list);
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.write(json.toString());;
			out.close();
		}else if(action.equals("refresh321")){
			ExcelHandler.clearLottery(Excel_PATH);
			extractList.clear();
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("This is doPost function");
		doGet(request,response);
	}
	
	private void initData(){
		System.out.println("初始化数据……");
    	for(int i=1;i<=countNum;i++){
        	lotteryList.add(i);
        }
    	ExcelHandler excelHandler = new ExcelHandler();
    	String path = "";
		try {
			path = this.getClass().getClassLoader().getResource("").toURI().getPath();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
    	path += "resources/员工名单.xlsx";
    	Excel_PATH = path;
    	try {
    		employeeList = excelHandler.readExcel(Excel_PATH);
    		extractList = excelHandler.readExtractXlsx(Excel_PATH);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
